using System;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Office.Interop.Outlook;
using Microsoft.TeamFoundation.Server;
using TXLooker.Common.Core;

namespace TXLooker.Core
{
    internal class OlProjectProviderWrapper : OlProviderWrapperBase, IProjectProvider
    {
        /// <summary>
        /// Data source
        /// </summary>
        protected ProjectProvider Source
        {
            get { return this.m_source; }
            private set
            {
                if( value == null ) throw new ArgumentException();
                this.m_source = value;

                this.Source.ProjectNameChanged += delegate { if( this.Folder != null ) this.Folder.Name = this.FolderName; };
            }
        }
        private ProjectProvider m_source = null;

        protected string FolderName
        {
            get
            {
                return this.Source.ProjectName == null ? "UNDEFINED PROJECT" : string.Format( "Project: {0}", this.Source.ProjectName );
            }
        }        

        /// <summary>
        /// Parent for current project wrapper
        /// </summary>
        protected OlServerProviderWrapper OlServerProvider
        {
            get { return this.Parent as OlServerProviderWrapper; }
        }

        #region Class Constructors

        public OlProjectProviderWrapper( OlServerProviderWrapper parent, ProjectProvider provider ) : base(parent)
        {
            this.Source = provider;
            this.InitFolder( parent.Folder );
        }

        #endregion Class Constructors

        #region IProjectProvider Members

        public IServerProvider ServerProvider
        {
            get { return this.OlServerProvider; }
        }
        public string ProjectName
        {
            get { return this.Source.ProjectName; }
        }
        public Uri ProjectUri
        {
            get { return this.Source.ProjectUri; }
        }
        public ProjectInfo GetProjectInfo()
        {
            return this.Source.GetProjectInfo();
        }

        protected override void UpdateData()
        {
            try
            {
                this.Source.Update();
                this.Folder.Name = this.FolderName;
            }
            catch( System.Exception ex ) 
            {
                Debug.WriteLine( ex.ToString() );
            }
        }

        #endregion


        public override void InitFolder( MAPIFolder parent )
        {
            string folderName = FolderName;
            MAPIFolder folder = Common.Outlook.OutlookGlobal.FindFolderByName(parent.Folders, folderName);
            if (folder == null)
            {
                folder = parent.Folders.Add(folderName, Missing.Value);
                folder.WebViewURL = this.ProjectWebUrl;
                folder.WebViewOn = true;
            }

            lock (this.SyncFolderObject)
            {
                this.Folder = folder;
            }
        }

        public string ProjectWebUrl
        {
            get
            {
                if( this.m_projectWebUrl == null )
                    this.m_projectWebUrl = string.Format( "http://{0}/sites/{1}", this.ServerProvider.ServerUri.Host, this.ProjectName );

                return this.m_projectWebUrl;
            }
        }
        private string m_projectWebUrl = null;
    }
}
